package club.xcgame520.okhttpdemo;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.File;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvFilePath;
    private Button bSelectFile;

    private Button bCreateTask;
    private Button bStartUpload;
    private Button bStopUpload;

    TaskUploadContinue taskUploadContinue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bSelectFile = findViewById(R.id.bSelectFile);
        bSelectFile.setOnClickListener(this);
        tvFilePath = findViewById(R.id.tvFilePath);

        bCreateTask = findViewById(R.id.bCreateTask);
        bCreateTask.setOnClickListener(this);

        bStartUpload = findViewById(R.id.bStartUpload);
        bStartUpload.setOnClickListener(this);

        bStopUpload = findViewById(R.id.bStopUpload);
        bStopUpload.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id == R.id.bSelectFile){
            Intent intentToPickPic = new Intent(Intent.ACTION_PICK, null);
            intentToPickPic.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
            startActivityForResult(intentToPickPic, 1);
        }
        if(id == R.id.bCreateTask){

            taskUploadContinue = new TaskUploadContinue(this);
            taskUploadContinue.configTask("test",new File(tvFilePath.getText().toString()));

        }

        if(id == R.id.bStartUpload){

            taskUploadContinue.start();

        }

        if(id == R.id.bStopUpload){

            taskUploadContinue.stop();

        }
    }

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
            Uri uri = data.getData();//得到uri，后面就是将uri转化成file的过程。
            final String filePath = FileUtil.getFilePathByUri(this, uri);
            tvFilePath.setText(filePath);
        }

    }

}
